package com.erp.erp_ui.Menum;

import com.erp.erp_entitys.Menum.MenuEntity2;
import com.erp.erp_entitys.Menum.MenuEntity2Example;
import com.erp.erp_servers.Menu.IMenuService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ResourceControl
 * @Description 菜单控制类
 * @Author songjizhen
 * @Date 2020-06-30 22:54
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "menu")
 public class MenuControl extends Base {
    @Autowired
    IMenuService menuService;
    /**
     * 根据主键删除菜单
     * @param code
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public R<Integer> delete( String code){
        try {
            MenuEntity2Example example=new MenuEntity2Example();
            MenuEntity2Example.Criteria criteria = example.createCriteria();
            criteria.andCodeEqualTo(code);
            int num = menuService.deleteByExample(example);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 新增资源
     * @param record
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> insert(@RequestBody MenuEntity2 record) {
        try {
            boolean exeitBycode = menuService.getExeitBycode(record.getCode());
            if(exeitBycode)record.setCode(System.currentTimeMillis()+"");
            int num = menuService.insertSelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }


    /**
     * 更新资源
     * @param record
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
     public R<Integer> update(@RequestBody MenuEntity2 record) {
        try {
            int num = menuService.updateByPrimaryKeySelective(record);
            return R.build(num);
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 获取菜单树
     * @return
     */
    @RequestMapping(value = "getTree",method = RequestMethod.POST)
    @ResponseBody
    public R<List<MenuEntity2>> getTree(){
        try {
            List<MenuEntity2> tree = menuService.getTree();

            return R.build(tree);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 检查code是否重复
     * @param code
     * @return
     */
    @RequestMapping(value = "getExeitBycode",method = RequestMethod.POST)
    @ResponseBody
    public R<Boolean> getExeitBycode(String code){
        try {
            boolean exeitBycode = menuService.getExeitBycode(code);
            return R.build(exeitBycode);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }
    /**
     * 根据CODE查询菜单
     * @param code  当前菜单code
     * @param flag   同级还是下级（T、X）
     * @param type   新增/修改(N、M)
     * @return
     */
    @RequestMapping(value = "getMenuByCode")
    @ResponseBody
    public R<Map<String,Object>> getMenuByCode(String code,String flag,String type){
        try {
            Map<String,Object> map=new HashMap<>();
            MenuEntity2Example example=new MenuEntity2Example();
            MenuEntity2Example.Criteria criteria=example.createCriteria();
            criteria.andCodeEqualTo(code);//查询当前数据
            List<MenuEntity2> menuEntity2s = menuService.selectByExample(example);
            criteria.getAllCriteria().clear();
            MenuEntity2 me2=menuEntity2s.get(0);
            if(type.equals("N")){//新增
                if(flag.equals("T")){//同级
                    map.put("pid",me2.getPid());
                    criteria.andCodeEqualTo(me2.getPid());//取上级节点
                    List<MenuEntity2> menuEntity2s2 = menuService.selectByExample(example);
                    MenuEntity2 me22=menuEntity2s2.get(0);
                    map.put("parentName",me22.getName());
                }else if(flag.equals("X")){//下级
                    map.put("pid",code);
                    map.put("parentName",me2.getName());
                }

            }else{//修改
                map.put("name",me2.getName());
                map.put("pid",me2.getPid());
                map.put("id",me2.getId());
                criteria.andCodeEqualTo(me2.getPid());//取上级节点
                List<MenuEntity2> menuEntity2s2 = menuService.selectByExample(example);
                MenuEntity2 me22=menuEntity2s2.get(0);
                map.put("parentName",me22.getName());
            }
            return R.build(map);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

    }
}
